//
//  ABToast.swift
//  B2BAutoziMall
//
//  Created by autozi01 on 16/3/19.
//  Copyright © 2016年 qeegoo. All rights reserved.
//

import UIKit


open class ToastSettingClass:NSObject {
    
    var textFont:CGFloat!
    var duration:CGFloat!
    var position:iToastGravity!
    override init(){
        super.init()
        textFont = 14
        duration = 2
        position = iToastGravity.Bottom
    }
}


// 枚举类型
public enum iToastGravity:Int{
    case Top = 1000001
    case Bottom
    case Center
    func typeName() -> String {
        return "iToastGravity"
    }
}

open class APPToast: NSObject {
    var toastSetting:ToastSettingClass!
    var frameMarginSize:CGFloat! = 10
    var frameSize:CGSize = CGSize(width:kScreenW - 40, height:265)
    var view:UIView!
    
    init(text:String,duration:TimeInterval) {
        super.init()
        toastSetting = ToastSettingClass()
        let textFont = toastSetting.textFont
        let size:CGSize = self.sizeWithString(string: text as NSString, font: UIFont.systemFont(ofSize: textFont!))
        
        let label:UILabel = UILabel (frame: CGRect(x:0, y:0, width:size.width, height:size.height))
        label.text = text
        label.font = UIFont.systemFont(ofSize: textFont!)
        label.numberOfLines = 0;
        label.textColor = UIColor.white
        let window:UIWindow = UIApplication.shared.keyWindow!
        //        if (window.rootViewController!){
        //            var currentViewCtrl:UIViewController = window.rootViewController!
        //        }
        let v:UIButton = UIButton(frame:CGRect(x:0, y:0, width:size.width + frameMarginSize, height:size.height + frameMarginSize))
        label.center = CGPoint(x:v.frame.size.width / 2, y:v.frame.size.height / 2);
        v.addSubview(label)
        
        v.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7)
        v.layer.cornerRadius = 5
        var point:CGPoint = CGPoint(x:window.frame.size.width/2, y:window.frame.size.height/5*4);
        point = CGPoint(x:point.x , y:point.y + 10);
        v.center = point
        v.tag = -100
        
        var bool = false
        for view1 in window.subviews {
            if view1.tag == -100 {
                bool = true
            }
        }
        if bool == false {
            window.addSubview(v)
        }
        view = v
        
        v.addTarget(self, action: #selector(APPToast.hideToast), for: UIControlEvents.touchDown)
        let timer:Timer = Timer(timeInterval: duration, target: self, selector: #selector(APPToast.hideToast), userInfo: nil, repeats: false)
        RunLoop.main.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
    }
    
    func sizeWithString(string:NSString, font:UIFont)->CGSize {
        let dic:NSDictionary = NSDictionary(object: font, forKey: NSFontAttributeName as NSCopying)
        //#0017460 modfiy by whq at 2016-04-09 begin
//        let options = NSStringDrawingOptions.TruncatesLastVisibleLine
        let options = NSStringDrawingOptions.usesLineFragmentOrigin
        //#0017460 modfiy by whq at 2016-04-09 end
        let rect:CGRect = string.boundingRect(with: frameSize, options:options, attributes: dic as [NSObject : AnyObject] as? [String : AnyObject], context: nil)
        return rect.size
    }
    
    func show(text: String,duration:TimeInterval = 2.0) {
        let _ =  APPToast(text: text,duration:duration)
    }
    
    func hideToast(){
        UIView.animate(withDuration: 0.2, animations: {
            () -> ()in
            self.view.alpha = 0
            }, completion: {
                (Boolean) -> ()in
                self.view.removeFromSuperview()
        })
        
    }
    
    required public init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
